package org.salatec.tapestry4e.text;

import org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML;
import org.eclipse.wst.html.core.text.IHTMLPartitions;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredTextPartitioner;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.sse.core.internal.text.IRegionComparible;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
import org.salatec.tapestry4e.util.Logger;

public class StructuredTextPartitionerTML extends
		StructuredTextPartitionerForHTML implements IStructuredTextPartitioner {

	private static final String HTML_MIME_TYPE = "text/html"; //$NON-NLS-1$
	private static final String XHTML_MIME_TYPE = "text/xhtml"; //$NON-NLS-1$
	private static final String XML_MIME_TYPE = "text/xml"; //$NON-NLS-1$
	public static final String TML_TAG_REGION = "TML_TAG_REGION";
	public static final String TML_ATTRIBUTE_REGION = "TML_ATTR_REGION";

	public StructuredTextPartitionerTML() {
		super();

	}

	@SuppressWarnings("restriction")
	@Override
	public String getPartitionType(ITextRegion region, int offset) {
		final String region_type = region.getType();
		if (region_type == DOMRegionContext.XML_TAG_NAME
				|| region_type == IHTMLPartitions.HTML_DEFAULT) {
			IStructuredDocumentRegion sdRegion = fStructuredDocument
					.getRegionAtCharacterOffset(offset);
			// String parentRegion = getParentName(sdRegion);
			// if (parentRegion != null && parentRegion.startsWith("t:"))
			// return TML_TAG_REGION;
		}
		return super.getPartitionType(region, offset);
	}

	/**
	 * @return
	 */
	public static String[] getConfiguredContentTypes() {
		return StructuredTextPartitionerForHTML.getConfiguredContentTypes();
	}

	@SuppressWarnings("restriction")
	private boolean isTmlTagRegion(ITextRegion region) {
		return ((IRegionComparible) fStructuredDocument)
				.regionMatchesIgnoreCase(region.getStart(), region
						.getTextLength(), "t:");
	}

	private String getParentName(IStructuredDocumentRegion sdRegion) {
		String result = "UNKNOWN"; //$NON-NLS-1$

		if (sdRegion != null) {
			ITextRegionList regions = sdRegion.getRegions();
			// only find parent names from a start tag
			if (regions.size() > 1) {
				ITextRegion r = regions.get(1);
				if (regions.get(0).getType().equals(
						DOMRegionContext.XML_TAG_OPEN)
						&& r.getType().equals(DOMRegionContext.XML_TAG_NAME)) {
					result = sdRegion.getText(r);
				}
			}
		}
		Logger.log(Logger.INFO, "GET PARENT RESULT: " + result);
		return result;
	}

}
